package cn.iocoder.yudao.module.common.controller.admin.comment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.comment.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.comment.CommentDO;
import cn.iocoder.yudao.module.common.service.comment.CommentService;

@Tag(name = "管理后台 - 评论")
@RestController
@RequestMapping("/common/comment")
@Validated
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    public CommonResult<CommentRespVO> createComment(@Valid @RequestBody CommentSaveReqVO createReqVO) {
        return success(commentService.createComment(createReqVO));
    }

    @PostMapping("/find/{targetId}/{targetType}/{type}/{prentId}")
    @Operation(summary = "创建评论")
    public CommonResult<List<CommentRespVO>> find(@PathVariable Long targetId,@PathVariable String targetType,@PathVariable Integer type,@PathVariable Long prentId) {
        return success(commentService.find(targetId,targetType,type,prentId));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评论")
    public CommonResult<Boolean> updateComment(@Valid @RequestBody CommentSaveReqVO updateReqVO) {
        commentService.updateComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteComment(@RequestParam("id") Long id) {
        commentService.deleteComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CommentRespVO> getComment(@RequestParam("id") Long id) {
        CommentDO comment = commentService.getComment(id);
        return success(BeanUtils.toBean(comment, CommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评论分页")
    public CommonResult<PageResult<CommentRespVO>> getCommentPage(@Valid CommentPageReqVO pageReqVO) {
        PageResult<CommentDO> pageResult = commentService.getCommentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CommentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评论 Excel")
    @OperateLog(type = EXPORT)
    public void exportCommentExcel(@Valid CommentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CommentDO> list = commentService.getCommentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "评论.xls", "数据", CommentRespVO.class,
                        BeanUtils.toBean(list, CommentRespVO.class));
    }

}